const { ccclass, property } = cc._decorator;
import baseView from '../../../../app/base/baseView';
import { autoProperty } from '../../../../app/decorator';
import { app } from '../../../../app/app';
import playerComponent from '../../../../ecs-component/playerComponent';
import velocityComponent from '../../../../ecs-component/velocityComponent';

@ccclass
export default class pageGame extends baseView {
    // protected mixinViews = ['paperGameextend'];
    @autoProperty
    player: cc.Node = null;

    @autoProperty
    startBtn: cc.Node = null;

    onLoad() {
        this.player.active = false;
        this.startBtn.active = true;

        this.startBtn.on('click', this.onStartBtnClick, this);

        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.keyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.keyUp, this);
    }

    private keyUp(event: cc.Event.EventKeyboard) {
        if (event.keyCode === cc.macro.KEY.a || event.keyCode === cc.macro.KEY.d) {
            app.ecs.getEntity(playerComponent).getComponent(velocityComponent).x = 0;
        }
    }


    private keyDown(event: cc.Event.EventKeyboard) {
        if (event.keyCode === cc.macro.KEY.a) {
            app.ecs.getEntity(playerComponent).getComponent(velocityComponent).x = -50;
        } else if (event.keyCode === cc.macro.KEY.d) {
            app.ecs.getEntity(playerComponent).getComponent(velocityComponent).x = 50;
        }
    }

    private onStartBtnClick() {
        this.player.active = true;
        this.startBtn.active = false;

        app.manager.game.startGame();
    }

    beforeShow(data, next) {
        app.manager.ui.show({ name: 'topLoading' })
        this.scheduleOnce(function () {
            app.manager.ui.hide({ name: 'topLoading' })
            next(true);
        }, 5);
    }

    onShow(data) {
        console.log('pageGame onShow', data);
    }

    onRefresh(data) {
        console.log('pageGame onRefresh', data);
    }

    onHide(data) {
        console.log('pageGame onHide', data);
    }
}